package com.jz.service.impl;

import com.jz.mapper.AdminMapper;
import com.jz.mapper.auto.TShopMapper;
import com.jz.mapper.auto.TStudentMapper;
import com.jz.module.dto.AdminDto;
import com.jz.module.dto.WxLoginDto;
import com.jz.module.entity.TShop;
import com.jz.module.entity.TShopExample;
import com.jz.module.entity.TStudent;
import com.jz.module.entity.TStudentExample;
import com.jz.service.LoginService;
import com.jz.util.ResultUtil;
import com.jz.util.SysCode;
import com.jz.util.UUIDUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    @SuppressWarnings("all")
    private TStudentMapper tStudentMapper;

    @Autowired
    @SuppressWarnings("all")
    private TShopMapper tShopMapper;

    @Autowired
    @SuppressWarnings("all")
    private AdminMapper adminMapper;

    @Override
    public AdminDto login(AdminDto adminDto) {
        if (adminDto == null || StringUtils.isAnyBlank(
                adminDto.getUsername(),
                adminDto.getPassword()
        )) {
            return null;
        }

        AdminDto result = adminMapper.select(adminDto);
        if (result == null) {
            return null;
        }
        if (StringUtils.equals(result.getPassword(), adminDto.getPassword())) {
            return result;
        }
        return null;
    }


    @Override
    public ResultUtil wxLogin(WxLoginDto dto) {
        if (dto == null || StringUtils.isAnyBlank(
                dto.getUsername(),
                dto.getPassword(),
                dto.getIsStuLogin()
        )) {
            return ResultUtil.paramError();
        }
        if (StringUtils.equals("true", dto.getIsStuLogin())) {
            TStudentExample example = new TStudentExample();
            example.createCriteria()
                    .andUsernameEqualTo(dto.getUsername())
                    .andPasswordEqualTo(dto.getPassword());
            List<TStudent> tStudents = tStudentMapper.selectByExample(example);
            if (tStudents == null || tStudents.size() < 1) {
                return ResultUtil.error("用户名或密码错误");
            }
            return ResultUtil.success(tStudents.get(0));
        } else {
            TShopExample example = new TShopExample();
            example.createCriteria()
                    .andUsernameEqualTo(dto.getUsername())
                    .andPasswordEqualTo(dto.getPassword());
            List<TShop> tShops = tShopMapper.selectByExample(example);
            if (tShops == null || tShops.size() < 1) {
                return ResultUtil.error("用户名或密码错误");
            }
            if ("0".equals(tShops.get(0).getAuthSuccess())) {
                return ResultUtil.error("请先联系管理员认证");
            }
            return ResultUtil.success(tShops.get(0));
        }
    }

    @Override
    public ResultUtil wxRegister(WxLoginDto dto) {
        if (dto == null || StringUtils.isAnyBlank(
                dto.getUsername(),
                dto.getPassword(),
                dto.getIsStuLogin()
        )) {
            return ResultUtil.paramError();
        }
        if (StringUtils.equals("true", dto.getIsStuLogin())) {

            TStudentExample example = new TStudentExample();
            example.createCriteria().andUsernameEqualTo(dto.getUsername());
            List<TStudent> tStudents = tStudentMapper.selectByExample(example);
            if (tStudents == null || tStudents.size() < 1) {
                TStudent insertDto = new TStudent();
                insertDto.setUsername(dto.getUsername());
                insertDto.setPassword(dto.getPassword());
                insertDto.setId(UUIDUtil.getUUID());
                insertDto.setNickName("学生" + UUIDUtil.getUUID().substring(0, 6));
                insertDto.setAge(0);
                insertDto.setAddress("");
                insertDto.setClasses("默认班级");
                insertDto.setSchool("默认学校");
                insertDto.setEmail("");
                insertDto.setAvatarUrl("../../resources/user.png");
                insertDto.setCreateTime(new Date());
                insertDto.setUpdateTime(new Date());
                insertDto.setMark("");
                insertDto.setSex("男");
                insertDto.setContent("");
                insertDto.setPhone("");

                int i = tStudentMapper.insert(insertDto);
                if (i > 0) {
                    insertDto.setPassword(null);
                    return ResultUtil.success(insertDto);
                }
                return ResultUtil.error("注册失败");
            }
            return ResultUtil.error("用户名已存在");
        } else {
            TShopExample example = new TShopExample();
            example.createCriteria().andUsernameEqualTo(dto.getUsername());
            List<TShop> tShops = tShopMapper.selectByExample(example);
            if (tShops == null || tShops.size() < 1) {
                TShop insertDto = new TShop();
                insertDto.setUsername(dto.getUsername());
                insertDto.setPassword(dto.getPassword());
                insertDto.setId(UUIDUtil.getUUID());
                insertDto.setShopName("店铺" + UUIDUtil.getUUID().substring(0, 6));

                insertDto.setCreateTime(new Date());
                insertDto.setUpdateTime(new Date());
                insertDto.setEmail("");
                insertDto.setAuthSuccess("0");
                insertDto.setName("");
                insertDto.setPhone("");
                insertDto.setImgUrl("../../resources/user.png");
                insertDto.setSchool("默认学校");
                insertDto.setShopAddress("");
                insertDto.setShopContent("");
                int i = tShopMapper.insert(insertDto);
                if (i > 0) {
                    insertDto.setPassword(null);
                    return ResultUtil.success(insertDto);
                }
                return ResultUtil.error("注册失败");
            }
            return ResultUtil.error("用户名已存在");
        }
    }

    @Override
    public ResultUtil upPsd(WxLoginDto dto) {
        if (dto == null || StringUtils.isAnyBlank(
                dto.getId(),
                dto.getOldPassword(),
                dto.getPassword(),
                dto.getType()
        )) {
            return ResultUtil.paramError();
        }
        if ("1".equals(dto.getType())) {
            // xuehseng
            TStudent tStudent = tStudentMapper.selectByPrimaryKey(dto.getId());
            if (tStudent == null) {
                return ResultUtil.error("数据不存在");
            }
            if (StringUtils.equals(tStudent.getPassword(), dto.getOldPassword())) {
                // 密码一致
                TStudent insertDto = new TStudent();
                insertDto.setUpdateTime(new Date());
                insertDto.setId(dto.getId());
                insertDto.setPassword(dto.getPassword());

                int i = tStudentMapper.updateByPrimaryKeySelective(insertDto);
                if (i > 0) {
                    return ResultUtil.success();
                }
                return ResultUtil.error(SysCode.UPDATE_ERROR);
            }
            return ResultUtil.error("密码错误");
        } else {
            // shop
            TShop tShop = tShopMapper.selectByPrimaryKey(dto.getId());
            if (tShop == null) {
                return ResultUtil.error("数据不存在");
            }
            if (StringUtils.equals(tShop.getPassword(), dto.getOldPassword())) {
                // 密码一致
                TShop insertDto = new TShop();
                insertDto.setUpdateTime(new Date());
                insertDto.setId(dto.getId());
                insertDto.setPassword(dto.getPassword());
                int i = tShopMapper.updateByPrimaryKeySelective(insertDto);
                if (i > 0) {
                    return ResultUtil.success();
                }
                return ResultUtil.error(SysCode.UPDATE_ERROR);
            }
            return ResultUtil.error("密码错误");
        }
    }
}
